package com.whut.transportation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;

import com.whut.transportation.adapter.GuDaoAdapter;
import com.whut.transportation.entity.AllTrackByStation;
import com.whut.transportation.entity.GuDao;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.utils.Constant;
/**
 * 货运员主界面查询
 * @author yuliyan
 */
public class CategoryActivity extends BaseActivity{
	// implements SwipeRefreshLayout.OnRefreshListener
	private SharedPreferences preferences;
	
	private ListView gudaos;
	private GuDaoAdapter guDaoAdapter;
	private List<GuDao> gudaolist;
	//private SwipeRefreshLayout mSwipeLayout; 
	private Spinner quming;
	private String qumingString;
	Editor editor;//偏好编辑
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.activity_main);
		setTitleBarView(false, "卸车", -1, false);
		preferences = getSharedPreferences("userId", Context.MODE_PRIVATE);
		editor = preferences.edit();
	}
	@Override
	protected void findViewById() {
		gudaos = (ListView) findViewById(R.id.gudaos);
		//mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
		quming = (Spinner) findViewById(R.id.quming);
	}

	@Override
	protected void afterFindViewId() {
		gudaolist = new ArrayList<GuDao>();
	}

	@Override
	protected void processLogic() {
	}

	private void getData(String q){
		HttpRequestVo vo = new HttpRequestVo();  
		vo.requestUrl = Constant.FIND_SCK_TRACK_ALL_BY_STATION ;
		vo.context = mContext;
		
		HashMap<String, String> req = new HashMap<String, String>();
		req.put("account",preferences.getString("account", null));
		req.put("name",preferences.getString("name", null));
		req.put("quming",q);
		vo.requestDataMap = req; 
		vo.parser = new JSONParser(AllTrackByStation.class);
		
		getDataFromServer(HttpManager.POST_MOTHOD, vo, 
				new DataCallback<HashMap<String, Object>>() {
					@Override
					public void onStart() {
					}
					@Override
					public void onFailed() {
						showNetErrorDialog();
					}
					@Override
					public void processData(
							HashMap<String, Object> paramObject,
							boolean paramBoolean) {
						AllTrackByStation allTrackByStation = (AllTrackByStation) paramObject.get("result");
						GuDao g = allTrackByStation.ListTrackByStation.get(0);
						if(!g.getGudao().equals("-1")){
							gudaolist.addAll(allTrackByStation.ListTrackByStation);
							if(gudaos.getAdapter()==null){
								guDaoAdapter = new GuDaoAdapter(mContext, gudaolist, R.layout.activity_detail_gudao_item);
								gudaos.setAdapter(guDaoAdapter);
							} else {
								guDaoAdapter.notifyDataSetChanged();
							}
						} else {
							Toast.makeText(mContext, "无待处理条目", Toast.LENGTH_SHORT).show();
						}
					}
					@Override
					public void onFinish() {
						closeProgressDialog();
					}
				});
	}
	
	@Override
	protected void setListener() {
		//mSwipeLayout.setOnRefreshListener(this);
		gudaos.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
				if(gudaolist.size()>0){
					Intent transInfoActivity = new Intent(CategoryActivity.this,TransInfoSckActivity.class);
					transInfoActivity.putExtra("gudao", gudaolist.get(position));
					startActivity(transInfoActivity);
				}
			}
		});
		
		quming.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				
				String q = parent.getItemAtPosition(position).toString();
				if(q.equals("港站散货")){
					qumingString = "SH";
				} else if(q.equals("港站矿码头")){
					qumingString = "KMT";
				} else if(q.equals("港站一区")){
					qumingString = "YQ";
				} else if(q.equals("东站")){
					qumingString = "DZ1";
				}
				if(qumingString!=null){
					if(gudaolist.size()>0){
						gudaolist.removeAll(gudaolist);
						guDaoAdapter.notifyDataSetChanged();
					}
					editor.putString("quming_1", qumingString);
					editor.commit();
					getData(qumingString);
				}
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
	}

	@Override
	protected void getDataAgain() {
		
	}

/*	@Override
	public void onRefresh() {
		if(qumingString!=null){
			getData(qumingString);
			guDaoAdapter.notifyDataSetChanged();
		}		
	}*/
}
